﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Telerik.Web.UI;

namespace BSCMFF.Web.UserControls
{
    public partial class EvaluationQuestions : System.Web.UI.UserControl
    {
        public event EventHandler EventChange;

        public Panel PublicPanelQuestion 
        {
            get
            {
                return PanelQuestions;
            }
            //set
            //{

            //}
        }

        public int NumberOfQuestions
        {
            get
            {
                return (int)RadNumericTextBoxNumberOfQuestions.Value;
            }
        }
        public int ScaleMin
        {
            get
            {
                return (int)RadNumericTextBoxMin.Value;
            }
        }
        public int ScaleMax
        {
            get
            {
                return (int)RadNumericTextBoxMax.Value;
            }
        }

        private int currentNumberOfQuestions;
        private static bool create = true;

        protected void Page_Load(object sender, EventArgs e)
        {

            //if (IsPostBack && ((Button)sender).ID == "bp")
            //{
            //    ButtonCreateQuestions_Click(sender, e);
            //}
            if (IsPostBack && PanelQuestions.Controls.Count == 1)
            {
                create = true;
                //if ((sender.GetType().Name != typeof(RadButton).Name))
                //{
                CreateQuestion(); // ButtonCreateQuestions_Click2(sender, e);
                //}
            }
        }

        protected void ButtonCreateQuestions_Click2(object sender, EventArgs e)
        {
            CreateQuestion();
        }

        public void CreateQuestion()
        {
            if (create)
            {
                Table t = new Table();
                t.ID = "TableQuestions";

                Label l = new Label();
                l.ID = "LabelHeader";
                l.Text = "Competencies";

                TableRow tr = new TableRow();
                TableCell tc = new TableCell();
                tc.Controls.Add(l);

                tr.Cells.Add(tc);



                for (int j = (int)RadNumericTextBoxMin.Value; j <= (int)RadNumericTextBoxMax.Value; ++j)
                {
                    Label ln = new Label();
                    ln.ID = "LabelNumber" + j.ToString();
                    ln.Text = j.ToString();

                    TableCell tcln = new TableCell();
                    tcln.Controls.Add(ln);
                    tcln.HorizontalAlign = HorizontalAlign.Center;
                    tr.Cells.Add(tcln);
                }



                t.Rows.Add(tr);

                currentNumberOfQuestions = (int)RadNumericTextBoxNumberOfQuestions.Value;

                for (int i = 0; i < RadNumericTextBoxNumberOfQuestions.Value; ++i)
                {
                    RadTextBox q = new RadTextBox();

                    q.ID = "Question" + i.ToString();
                    q.Width = 500;

                    TableRow trQ = new TableRow();
                    TableCell tcQ = new TableCell();
                    tcQ.Controls.Add(q);
                    trQ.Cells.Add(tcQ);

                    //PanelQuestions.Controls.Add(q);

                    //PanelQuestions.Controls.Add(new LiteralControl("<span>"));


                    for (int j = (int)RadNumericTextBoxMin.Value; j <= (int)RadNumericTextBoxMax.Value; ++j)
                    {
                        RadioButton rb = new RadioButton();

                        string si = i < 10 ? "0" + i.ToString() : i.ToString();
                        string sj = j < 10 ? "0" + j.ToString() : j.ToString();

                        rb.ID = "Radio" + si + sj;
                        rb.GroupName = "Group" + i.ToString();
                        rb.Enabled = false;
                        //PanelQuestions.Controls.Add(rb);

                        TableCell tcCH = new TableCell();
                        tcCH.Controls.Add(rb);

                        trQ.Cells.Add(tcCH);
                    }

                    //PanelQuestions.Controls.Add(new LiteralControl("</span>"));
                    //PlaceHolder ph = new PlaceHolder();

                    //ph.ID = "PlaceHolder" + i.ToString();

                    //PanelQuestions.Controls.Add(ph);

                    //PanelQuestions.Controls.Add(new LiteralControl("<br />"));
                    // trQ.Cells.Add(tcCH);
                    t.Rows.Add(trQ);
                }

                PanelQuestions.Controls.Add(t);
            }
            else
            {
                //ButtonCreateQuestions_Click2(sender, new EventArgs());
                if (EventChange != null)
                {
                    EventChange(this, new EventArgs());
                }
            }

            create = false;
        }

        public void CreateQuestionLoad(int number, int min, int max)
        {
            RadNumericTextBoxMin.Value = (double?)min;
            RadNumericTextBoxMax.Value = (double?)max;
            RadNumericTextBoxNumberOfQuestions.Value = (double?)number;


            Table t = new Table();
            t.ID = "TableQuestions";

            Label l = new Label();
            l.ID = "LabelHeader";
            l.Text = "Questions";

            TableRow tr = new TableRow();
            TableCell tc = new TableCell();
            tc.Controls.Add(l);

            tr.Cells.Add(tc);



            for (int j = (int)RadNumericTextBoxMin.Value; j <= (int)RadNumericTextBoxMax.Value; ++j)
            {
                Label ln = new Label();
                ln.ID = "LabelNumber" + j.ToString();
                ln.Text = j.ToString();

                TableCell tcln = new TableCell();
                tcln.Controls.Add(ln);
                tcln.HorizontalAlign = HorizontalAlign.Center;
                tr.Cells.Add(tcln);
            }



            t.Rows.Add(tr);

            currentNumberOfQuestions = (int)RadNumericTextBoxNumberOfQuestions.Value;

            for (int i = 0; i < RadNumericTextBoxNumberOfQuestions.Value; ++i)
            {
                RadTextBox q = new RadTextBox();

                q.ID = "Question" + i.ToString();
                q.Width = 500;

                TableRow trQ = new TableRow();
                TableCell tcQ = new TableCell();
                tcQ.Controls.Add(q);
                trQ.Cells.Add(tcQ);

                //PanelQuestions.Controls.Add(q);

                //PanelQuestions.Controls.Add(new LiteralControl("<span>"));


                for (int j = (int)RadNumericTextBoxMin.Value; j <= (int)RadNumericTextBoxMax.Value; ++j)
                {
                    RadioButton rb = new RadioButton();

                    string si = i < 10 ? "0" + i.ToString() : i.ToString();
                    string sj = j < 10 ? "0" + j.ToString() : j.ToString();

                    rb.ID = "Radio" + si + sj;
                    rb.GroupName = "Group" + i.ToString();
                    rb.Enabled = false;
                    //PanelQuestions.Controls.Add(rb);

                    TableCell tcCH = new TableCell();
                    tcCH.Controls.Add(rb);

                    trQ.Cells.Add(tcCH);
                }

                //PanelQuestions.Controls.Add(new LiteralControl("</span>"));
                //PlaceHolder ph = new PlaceHolder();

                //ph.ID = "PlaceHolder" + i.ToString();

                //PanelQuestions.Controls.Add(ph);

                //PanelQuestions.Controls.Add(new LiteralControl("<br />"));
                // trQ.Cells.Add(tcCH);
                t.Rows.Add(trQ);
            }

            PanelQuestions.Controls.Add(t);
            
        }

        //protected void ButtonCreateQuestions_Click(object sender, EventArgs e)
        //{
        //    if (create)
        //    {
        //        Label l = new Label();
        //        l.ID = "LabelHeader";
        //        l.Width = 500;
        //        l.Text = "Questions";


        //        currentNumberOfQuestions = (int)RadNumericTextBoxNumberOfQuestions.Value;

        //        for (int i = 0; i < RadNumericTextBoxNumberOfQuestions.Value; ++i)
        //        {
        //            RadTextBox q = new RadTextBox();

        //            q.ID = "Question" + i.ToString();
        //            q.Width = 500;

        //            PanelQuestions.Controls.Add(q);

        //            PanelQuestions.Controls.Add(new LiteralControl("<span>"));

        //            for (int j = (int)RadNumericTextBoxMin.Value; j <= (int)RadNumericTextBoxMax.Value; ++j)
        //            {
        //                RadioButton rb = new RadioButton();

        //                string si = i < 10 ? "0" + i.ToString() : i.ToString();
        //                string sj = j < 10 ? "0" + j.ToString() : j.ToString();

        //                rb.ID = "Radio" + si + sj;
        //                rb.GroupName = "Group" + i.ToString();
        //                PanelQuestions.Controls.Add(rb);
        //            }

        //            PanelQuestions.Controls.Add(new LiteralControl("</span>"));
        //            //PlaceHolder ph = new PlaceHolder();

        //            //ph.ID = "PlaceHolder" + i.ToString();

        //            //PanelQuestions.Controls.Add(ph);

        //            PanelQuestions.Controls.Add(new LiteralControl("<br />"));

        //        }
        //    }
        //    create = false;
        //}



        //protected void bp_Click(object sender, EventArgs e)
        //{
        //    //ButtonCreateQuestions_Click(sender, e);
        //    create = true;
        //}
    }
}